"""

查看肝脏区域slice占据整体slice的比例
"""

import os
import sys

import numpy as np

sys.path.append(os.path.split(sys.path[0])[0])

from tqdm import tqdm
import SimpleITK as sitk
from utilities.StatisticTime import getTime
from config import parameter as para


def analysePhoto(file_list, file_description = ""):
    # 查看图片级别的百分比
    total_slice = 0.0
    total_liver_slice = 0.0
    for file in file_list:
        seg = sitk.ReadImage(file)
        seg_array = sitk.GetArrayFromImage(seg)

        # if (1 in seg_array) or (2 in seg_array):#计算肝脏或肿瘤
        if (1 in seg_array):
            total_liver_slice += 1

        total_slice += 1
    print(file_description + ":", total_liver_slice, total_slice, total_liver_slice / total_slice)
def analysePixel(file_list, file_description = ""):
    # 查看像素级别的百分比
    total_slice = 0.0
    total_liver_slice = 0.0
    for file in file_list:
        seg = sitk.ReadImage(file)
        seg_array = sitk.GetArrayFromImage(seg)

        total_liver_slice += np.sum(seg_array==2)

    total_slice = 512*512*len(file_list)#每个图片的像素是512*512*1
    print(file_description + ":", total_liver_slice, total_slice, total_liver_slice / total_slice)


if __name__ == '__main__':
    root = para.cut2d_save_path
    id_path_list = [para.val2d_id_path,
                    para.test2d_id_path,
                    para.train_label2d_id_path,
                    para.train_unlabeled2d_id_path]
    for id_path in id_path_list:
        with open(id_path, 'r') as f:
            ids = f.read().splitlines()
        file_list = [os.path.join(root, id.split()[1]) for id in ids]
        analysePhoto(file_list, file_description = os.path.basename(id_path))
